package pro.reznick.geoalarm;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import pro.reznick.geoalarm.exceptions.GeoAlarmValidationException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import pro.reznick.geoalarm.map.GetCoordsActivity;
import pro.reznick.geoalarm.map.MapConstants;

public class ManageAlarmActivity extends GeoAlarmBaseActivity
{

    private GeoAlarmAction action;
    private int alarmId;
    private boolean gotResult = false;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.managealarm);
    }

    @Override
    protected void onControllerBoundEvent()
    {
        action = (GeoAlarmAction) getIntent().getExtras().get("action");
        switch (action)
        {

            case ADD_ALARM:
                if (!gotResult)
                    startMapActivity(MapConstants.REQUEST_CODE_NEW_COORDINATES);
                break;
            case EDIT_ALARM:

                alarmId = getIntent().getIntExtra("alarmId", -1);
                if (alarmId > 0)
                {
                    AlarmData alarm = controllerTrait.controller.getAlarm(alarmId);
                    if (alarm != null)
                    {
                        ((TextView) findViewById(R.id.txtLatitude)).setText(Double
                                .toString(alarm.getLatitude()));
                        ((TextView) findViewById(R.id.txtLongitude))
                                .setText(Double.toString(alarm.getLongitude()));
                        ((TextView) findViewById(R.id.txtDistance)).setText(Integer
                                .toString(alarm.getDistanceInMeters()));
                        ((TextView) findViewById(R.id.txtName)).setText(alarm
                                .getName());
                        ((TextView) findViewById(R.id.txtMessage)).setText(alarm
                                .getText());

                    }
                    else
                    {
                        Toast t = Toast.makeText(this,
                                "Data corruption encountered", Toast.LENGTH_SHORT);
                        t.show();
                        finish();
                    }

                }
                else
                {
                    Toast t = Toast.makeText(this, "Failed loading alarm",
                            Toast.LENGTH_SHORT);
                    t.show();
                    finish();
                }
        }

        final Activity _this = this;

        Button button = (Button) findViewById(R.id.btnSave);
        button.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {

                String latitude = ((TextView) findViewById(R.id.txtLatitude))
                        .getText().toString();
                String longitude = ((TextView) findViewById(R.id.txtLongitude))
                        .getText().toString();
                String distanceInMeters = ((TextView) findViewById(R.id.txtDistance))
                        .getText().toString();
                String name = ((TextView) findViewById(R.id.txtName)).getText()
                        .toString();
                String message = ((TextView) findViewById(R.id.txtMessage))
                        .getText().toString();

                Toast t;
                try
                {
                    if (action == GeoAlarmAction.ADD_ALARM)
                    {
                        controllerTrait.controller.createNewAlarm(latitude, longitude,
                                distanceInMeters, name, message);
                    }
                    else
                    {
                        controllerTrait.controller.updateAlarm(alarmId, latitude, longitude,
                                distanceInMeters, name, message);
                    }
                    t = Toast.makeText(_this, R.string.alarm_saved,
                            Toast.LENGTH_SHORT);
                    t.show();
                    finish();
                }
                catch (GeoAlarmValidationException e)
                {
                    t = Toast.makeText(_this, e.getMessage(),
                            Toast.LENGTH_SHORT);
                    t.show();
                }
            }

        });

        Button btnCancel = (Button) findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                finish();
            }
        });

        Button buttonGetCoords = (Button) findViewById(R.id.btnEditCoordinates);
        buttonGetCoords.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                startMapActivity(MapConstants.REQUEST_CODE_EDIT_COORDINATES);
            }

        });

    }

    private void startMapActivity(int requestCode)
    {
        Intent intent = new Intent(this, GetCoordsActivity.class);
        if (requestCode == MapConstants.REQUEST_CODE_EDIT_COORDINATES)
        {
            intent.putExtra(MapConstants.LATITUDE_EXTRA, Double.parseDouble(((TextView) findViewById(R.id.txtLatitude))
                    .getText().toString()));
            intent.putExtra(MapConstants.LONGITUDE_EXTRA, Double.parseDouble(((TextView) findViewById(R.id.txtLongitude))
                    .getText().toString()));
        }
        intent.putExtra(MapConstants.REQUEST_CODE, requestCode);
        startActivityForResult(intent, requestCode);
    }

    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data)
    {
        if (resultCode == RESULT_OK)
        {

            ((TextView) findViewById(R.id.txtLatitude)).setText(Double
                    .toString(data.getDoubleExtra(MapConstants.LATITUDE_EXTRA, 0.0)));
            ((TextView) findViewById(R.id.txtLongitude))
                    .setText(Double.toString(data.getDoubleExtra(MapConstants.LONGITUDE_EXTRA, 0.0)));


        }

        gotResult = true;
    }


}
